// 实现这个项目的构建任务
const sass = require("sass");
const loadGruntTasks = require("load-grunt-tasks");

module.exports = (grunt) => {
  grunt.initConfig({
    clean: {
      options: {
        "no-write": false,
      },
      main: ["dist", "temp"],
    },
    sass: {
      options: {
        sourceMap: true,
        implementation: sass,
      },
      main: {
        files: {
          "dist/assets/styles/main.css": "src/assets/styles/main.scss",
        },
      },
    },
    babel: {
      options: {
        sourceMap: true,
        presets: ["@babel/preset-env"],
      },
      main: {
        files: {
          "dist/assets/scripts/main.js": "src/assets/scripts/main.js",
        },
      },
    },
    swigtemplates: {
      options: {
        defaultContext: {
          menus: [
            {
              name: "Home",
              icon: "aperture",
              link: "index.html",
            },
            {
              name: "Features",
              link: "features.html",
            },
            {
              name: "About",
              link: "about.html",
            },
            {
              name: "Contact",
              link: "#",
              children: [
                {
                  name: "Twitter",
                  link: "https://twitter.com/w_zce",
                },
                {
                  name: "About",
                  link: "https://weibo.com/zceme",
                },
                {
                  name: "divider",
                },
                {
                  name: "About",
                  link: "https://github.com/zce",
                },
              ],
            },
          ],
          pkg: require("./package.json"),
          date: new Date(),
        },
        templatesDir: "src",
      },
      production: {
        dest: "dist/",
        src: ["src/**/*.html"],
      },
    },
    htmlmin: {
      dist: {
        // Target
        options: {
          // Target options
          removeComments: true,
          collapseWhitespace: true,
        },
        files: [
          {
            expand: true,
            cwd: "dist",
            src: ["**/*.html", "*.html"],
            dest: "dist",
          },
        ],
      },
    },
    useref: {
      // specify which files contain the build blocks
      html: "dist/**/*.html",
      // explicitly specify the temp directory you are working in
      // this is the the base of your links ( "/" )
      temp: "dist",
    },
    // concat: {
    //   // options: {
    //   //   separator: ";",
    //   // },
    //   js: {
    //     // src: [
    //     //   "node_modules/jquery/dist/jquery.js",
    //     //   "node_modules/popper.js/dist/umd/popper.js",
    //     //   "node_modules/bootstrap/dist/js/bootstrap.js",
    //     // ],
    //     dest: "dist/assets/scripts/vendor.js",
    //   },
    //   css: {
    //     // src: ["node_modules/bootstrap/dist/css/bootstrap.css"],
    //     dest: "dist/assets/styles/vendor.css",
    //   },
    // },
    // uglify: {
    //   main: {
    //     files: {
    //       "dist/assets/scripts/vendor.js": ["dist/assets/scripts/vendor.js"],
    //       "dist/assets/styles/vendor.css": ["dist/assets/styles/vendor.css"],
    //     },
    //   },
    // },
    cssmin: {
      options: {
        mergeIntoShorthands: false,
        roundingPrecision: -1,
        sourceMap: true,
        report: "min",
      },
      target: {
        files: [
          {
            expand: true,
            cwd: "dist/assets/styles",
            src: ["*.css", "!*.min.css"],
            dest: "dist/assets/styles",
            ext: ".min.css",
          },
        ],
      },
    },
    imagemin: {
      dynamic: {
        options: {
          optimizationLevel: 3,
          svgoPlugins: [{ removeViewBox: false }],
        },
        files: [
          {
            expand: true,
            cwd: "src/",
            src: ["assets/images/*.{png,jpg,gif,svg}", "assets/fonts/*"],
            dest: "dist/",
          },
        ],
      },
    },
    copy: {
      options: {},
      main: {
        files: [{ expand: true, cwd: "public", src: "**", dest: "dist/" }],
      },
    },
    watch: {
      js: {
        files: ["src/assets/scripts/*.js"],
        tasks: ["babel"],
      },
      css: {
        files: ["src/assets/styles/*.scss"],
        tasks: ["sass"],
      },
    },
  });

  // grunt.loadNpmTasks('grunt-sass')
  loadGruntTasks(grunt); // 自动加载所有的 grunt 插件中的任务

  grunt.registerTask("default", [
    "clean",
    "sass",
    "babel",
    "swigtemplates",
    "htmlmin",
    // "useref",
    // "concat",
    // "uglify",
    "cssmin",
    "imagemin",
    "copy",
  ]);
};
